package com.loyal.mwm.libs.api.login

import android.os.Build
import com.base.kit.DevUtil
import com.loyal.mwm.data.ResponseData
import com.loyal.mwm.libs.api.ApiActionServer
import com.loyal.mwm.libs.api.BaseRepository
import com.loyal.mwm.libs.api.ServerApi
import com.loyal.rx3.RetroManager

class LoginRepository :BaseRepository() {
    private var actionServer: ApiActionServer
    private val retroManager = RetroManager.getInstance()

    init {
        val config = ServerApi.config()
        retroManager.build(config)
        actionServer = retroManager.createServer(ApiActionServer::class.java)
    }

    override fun updateBaseUrl() {
        val config = ServerApi.config()
        retroManager.build(config)
        actionServer = retroManager.createServer(ApiActionServer::class.java)
    }

    suspend fun register(): ResponseData<String?> {
        return request(What.TOKEN) {
            val model = Build.MODEL
            val serial = DevUtil.serial()
            actionServer.register(ServerApi.tokenMap(model, serial))
        }
    }

    suspend fun login(token: String?): ResponseData<String?> {
        return request(What.MENU) {
            val ipAdd = DevUtil.ipv4()
            actionServer.loginByJson(ServerApi.menuMap(ipAdd, token))
        }
    }

    suspend fun loginByJson(): ResponseData<String?> {
        return request(block = {
            val paramMap = ServerApi.asyncMap()
            actionServer.loginByJson(paramMap)
        })
    }
}